package com.example.dingdong.service;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileUploadAndDownloadService {
	
	private static final Logger log = LoggerFactory.getLogger(FileUploadAndDownloadService.class);
	/**
	 * 
	 * @Title: uploadFile
	 * @Description:上传图片 
	 * @Author coding
	 * @DateTime 2021-06-04 20:38:19
	 * @param file
	 * @param fileName
	 * @return
	 */
	public Boolean uploadFile(MultipartFile file, String fileName) {
		String folderName = fileName.substring(0, fileName.lastIndexOf("/"));
		File folder = new File(folderName);
		// 检查文件夹是否创建
		if (!folder.exists()) {
			folder.mkdirs();
		}
		File uploadFile = new File(fileName);
		try {
			// 保存文件
			// 使用此方法保存必须要绝对路径且文件夹必须已存在,否则报错
			file.transferTo(uploadFile);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

}
